package com.example.myapplication;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class AddNoteDialog extends DialogFragment {
    private EditText editTextTitle;
    private EditText editTextContent;

    public interface OnNoteAddedListener {
        void onNoteAdded(Note note);
    }

    private OnNoteAddedListener listener;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            listener = (OnNoteAddedListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnNoteAddedListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_add_note, null);

        editTextTitle = view.findViewById(R.id.edit_text_title);
        editTextContent = view.findViewById(R.id.edit_text_content);

        builder.setView(view)
                .setTitle("添加新笔记")
                .setPositiveButton("添加", (dialog, which) -> {
                    String title = editTextTitle.getText().toString().trim();
                    String content = editTextContent.getText().toString().trim();

                    if (!title.isEmpty()) {
                        Note note = new Note(title, content, System.currentTimeMillis());
                        listener.onNoteAdded(note);
                    }
                })
                .setNegativeButton("取消", null);

        return builder.create();
    }
}